package com.example.lanouhn.bantangapp.activities.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FragmentSerach_deillist;
import com.example.lanouhn.bantangapp.activities.contants.Search_deildelist;
import com.example.lanouhn.bantangapp.activities.contants.Search_deillist_item;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_Search_deillist;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/30.
 */
public class Fragment_Search_item_detaieledlist extends Fragment {
    private List<Search_deildelist> deildelists = new ArrayList<>();//实体类集合
    private Adapter_FragmentSerach_deillist adapter_fragmentSerach_deillist;//listview的adapter
    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View veiw = inflater.inflate(R.layout.fragment_item_detailedlist,container,false);
        recyclerView= (RecyclerView) veiw.findViewById(R.id.recyclerview);
        return veiw;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDataToPost();
    }

    //post解析数据
    private void initDataToPost() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                String url = Search_deillist_item.SEARCHDEILLISTURL;//获取网络地址
                String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2425199&oauth_token=dc743c52935a58a1391eab89d551e75c&track_deviceid=868596020655823&track_device_info=m2+note&channel_name=UMENG_CHANNEL&app_installtime=1468811189&app_versions=5.8.4&os_versions=5.1&screensize=1080&v=16";


                String result = HttpUtils.doPost(url,params);

                if (null!=result&&result.length()>0){
                    deildelists = JsonUtils_Search_deillist.getsrachdeList(result);
                    handler.sendEmptyMessage(1);
                }else{
                    handler.sendEmptyMessage(0);
                }

            }
        }).start();
    }

    //验证数据的线程
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //失败
                    Toast.makeText(getActivity(), R.string.fail, Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    //成功
                    //绑定Adapter并在recyclerview上展示
                    adapter_fragmentSerach_deillist = new Adapter_FragmentSerach_deillist( getActivity(),deildelists);

                    recyclerView.setAdapter(adapter_fragmentSerach_deillist);
                    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(staggeredGridLayoutManager);
                    break;
            }
        }
    };
}